package com.emm.yixun.website.controller.estate;
//框架包
import java.util.ArrayList;
import java.util.Date;

import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.repository.ProcessDefinitionQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

//变化的引用
import com.emm.yixun.common.model.ProjectProcdef;
import com.emm.yixun.common.model.User;
import com.emm.yixun.common.model.dto.ProjectProcdefDto;
import com.emm.yixun.common.page.Page;
//common
import com.emm.yixun.common.page.PageUtil;
import com.emm.yixun.common.response.Response;
import com.emm.yixun.common.response.ResponseList;
import com.emm.yixun.estate.service.ProjectProcdefServiceFacade;
//website
import com.emm.yixun.website.common.ajax.AjaxRes;
import com.emm.yixun.website.common.utils.base.Const;
import com.emm.yixun.website.common.utils.security.ShiroUtil;
import com.emm.yixun.website.controller.base.BaseController;




@Controller
@RequestMapping("/estate/projectProcdef/")
public class ProjectProcdefController extends BaseController<ProjectProcdef>{
	
	@Autowired
	private ProjectProcdefServiceFacade projectProcdefServiceFacade;
	
	@Autowired
	private RepositoryService repositoryService;
	
	@RequestMapping(value="index")
	public String index(ModelMap map,String projectId){
		if(doSecurityIntercept(Const.RESOURCES_TYPE_MENU)){
			//查询菜单按钮权限
			map.put("permitBtn", getPermissionBtnArray());
			map.put("projectId", projectId);
			return "/estate/projectProcdef/list";
		}
		return Const.NO_AUTHORIZED_URL;
	}
	
	@RequestMapping(value="setProjectProcdef")
	public String setProjectProcdef(String id,String procdefId,ModelMap map){
		//查询菜单按钮权限
		map.put("id", id);
		map.put("procdefId", procdefId);
		return "/estate/projectProcdef/update";
	}
	
	//新增返回实体对象
	@RequestMapping(value="add", method=RequestMethod.POST)
	@ResponseBody
    public AjaxRes  add(ProjectProcdefDto dto){
    	AjaxRes ar=getAjaxRes();
    	
    	/*
    	 *1.判断权限 
    	 *2.调用新增接口
    	 *3.返回信息
    	*/
        if(ar.setNoAuth(doSecurityIntercept(Const.RESOURCES_TYPE_BUTTON,"estate:projectProcdef:add"))){
        	try {
        		User user=ShiroUtil.getCurrentUser();
        		dto.setMerchantId(user.getMerchantId());
        		dto.setCreateUserId(user.getId());
        		dto.setDelFlag("0");
        		dto.setUpdateTime(new Date());
				Response<ProjectProcdefDto> response=projectProcdefServiceFacade.save(dto);
				if(response.isSuccess()){
					ar.setSucceedMsg(Const.SAVE_SUCCEED);
				}else{
					ar.setFailMsg(Const.SAVE_FAIL);
				}
			} catch (Exception e) {
				logger.error(e.toString(),e);
				ar.setFailMsg(Const.SAVE_FAIL);
			}
    	}
    	return ar;
    }
	//查看
	@RequestMapping(value="view", method=RequestMethod.POST)
	@ResponseBody
    public AjaxRes  view(Long id){
    	AjaxRes ar=getAjaxRes();
    	/*
    	 *1.判断权限 
    	 *2.调用查看接口
    	 *3.返回信息
    	*/
        if(ar.setNoAuth(doSecurityIntercept(Const.RESOURCES_TYPE_BUTTON,"estate:projectProcdef:view"))){
        	try {
				Response<ProjectProcdef> response=projectProcdefServiceFacade.findById(id);
				if(response.isSuccess()){
					ar.setSucceed(response.getEntity());
				}else {
					ar.setFailMsg(Const.DATA_FAIL);
				}
			} catch (Exception e) {
				logger.error(e.toString(),e);
				ar.setFailMsg(Const.DATA_FAIL);
			}
    	}
    	return ar;
    }
	
	//修改
	@RequestMapping(value="edit", method=RequestMethod.POST)
	@ResponseBody
    public AjaxRes  edit(ProjectProcdefDto dto){
    	AjaxRes ar=getAjaxRes();
    	/*
    	 *1.判断权限 
    	 *2.调用修改接口
    	 *3.返回信息
    	*/
    	if(ar.setNoAuth(doSecurityIntercept(Const.RESOURCES_TYPE_BUTTON,"estate:projectProcdef:edit"))){
	    	try {
	    		//查询流程key
	    		String procdefId=dto.getProcdefId();
	    		ProcessDefinitionQuery query = repositoryService.createProcessDefinitionQuery();
	    		ProcessDefinition processDefinition=query.processDefinitionId(procdefId).singleResult();
	    		dto.setProcName(processDefinition.getName());
	    		dto.setProcKey(processDefinition.getKey());
	    		//查询是否存在
	    		ProjectProcdefDto pdd=new ProjectProcdefDto();
	    		pdd.setProcKey(processDefinition.getKey());
	    		ResponseList<ProjectProcdef> ppResponseList=projectProcdefServiceFacade.findByDto(pdd);
	    		if(ppResponseList.isSuccess() && null!=ppResponseList.getEntity() && ppResponseList.getEntity().size()>0){
	    			ar.setFailMsg(processDefinition.getName()+"流程已存在！");
	    			return ar;
	    		}
	    		Response<Integer> response=projectProcdefServiceFacade.updateByPrimaryKey(dto, true);
				if(response.isSuccess()){
					ar.setSucceedMsg(Const.UPDATE_SUCCEED);
				}else{
					ar.setFailMsg(Const.UPDATE_FAIL);
				}
			} catch (Exception e) {
				logger.error(e.toString(),e);
				ar.setFailMsg(Const.UPDATE_FAIL);
			}
    	}
    	return ar;
    }
	
	//删除
	@RequestMapping(value="delete", method=RequestMethod.POST)
	@ResponseBody
    public AjaxRes  delete(Long id){
    	AjaxRes ar=getAjaxRes();
    	/*
    	 *1.判断权限 
    	 *2.调用查看接口
    	 *3.返回信息
    	*/
        if(ar.setNoAuth(doSecurityIntercept(Const.RESOURCES_TYPE_BUTTON,"estate:projectProcdef:delete"))){
        	try {
        		Response<Integer> response=projectProcdefServiceFacade.deleteByPrimaryKey(id);
				if(response.isSuccess()){
					ar.setSucceedMsg(Const.DEL_SUCCEED);
				}else {
					ar.setFailMsg(Const.DEL_FAIL);
				}
			} catch (Exception e) {
				logger.error(e.toString(),e);
				ar.setFailMsg(Const.DEL_FAIL);
			}
    	}
    	return ar;
    }
	
	
	
	//分页查询
	@RequestMapping(value="list", method=RequestMethod.POST)
	@ResponseBody
    public AjaxRes  list(ProjectProcdefDto dto,Integer pageSize,Integer pageNum){
    	AjaxRes ar=getAjaxRes();
    	/*
    	 *1.判断权限 
    	 *2.调用分页查询接口
    	 *3.返回信息
    	*/
        if(ar.setNoAuth(doSecurityIntercept(Const.RESOURCES_TYPE_BUTTON,"estate:projectProcdef:list"))){
        	try {
        		User user=ShiroUtil.getCurrentUser();
        		dto.setMerchantId(user.getMerchantId());
        		Page<ProjectProcdef> response=projectProcdefServiceFacade.findPageByDto(dto, PageUtil.begin(pageNum, pageSize),pageSize);
                if(response.getSuccess()){
                    if(null==response.getRows())response.setRows(new ArrayList<ProjectProcdef>());
                    ProcessDefinitionQuery query=repositoryService.createProcessDefinitionQuery();
                    for(ProjectProcdef pp : response.getRows()){
                    	ProcessDefinition processDefinition= query.processDefinitionKey(pp.getProcKey()).orderByProcessDefinitionVersion().desc().list().get(0);
                    	/*ProcessDefinition processDefinition= query.processDefinitionId(pp.getProcdefId()).singleResult();*/
                    	if(null==processDefinition)continue;
                    	pp.setProcImageName(processDefinition.getDiagramResourceName());
                    	pp.setProcdefId(processDefinition.getId());
                    }
                	response.setPageNumber(pageNum);
                	ar.setSucceed(response);
                }else{
                	ar.setFailMsg(Const.DATA_FAIL);
                }
			} catch (Exception e) {
				logger.error(e.toString(),e);
				ar.setFailMsg(Const.DATA_FAIL);
			}
    	}
    	return ar;
    }
	

	
	
}
